<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/21
 * Time: 11:26
 */

namespace app\admin\controller;
use app\admin\model\FreightModel;
use app\admin\model\FreightTplModel;
use app\admin\model\LogisticsModel;
use app\admin\service\AddressService;
use app\admin\service\ExpressService;
use cmf\controller\AdminBaseController;

class FreightController extends AdminBaseController
{
    /**
     * 运费管理
     * @adminMenu(
     *     'name'   => '运费管理',
     *     'parent' => 'admin/Setting/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '运费管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $freight_tpl=new FreightTplModel();
        $freight_tpl_list=$freight_tpl->get_freight_tpl();
        $this->assign('freight_tpl_list',$freight_tpl_list);
        return $this->fetch('tpl_index');
    }

    /**
     * 添加运费管理
     * @adminMenu(
     *     'name'   => '添加运费管理',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '添加运费管理',
     *     'param'  => ''
     * )
     */
    public function add_freight_tpl()
    {
        if(request()->isPost()){
            $param=request()->param();
            $freight_tpl=new FreightTplModel();
            $freight_tpl->save($param);
            $this->success('添加成功',url('admin/freight/index'));
        }else{
            return $this->fetch();
        }

    }
    /**
     * 编辑运费管理
     * @adminMenu(
     *     'name'   => '编辑运费管理',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '编辑运费管理',
     *     'param'  => ''
     * )
     */
    public function edit_freight_tpl()
    {
        //todo

    }
    /**
     * 删除运费管理
     * @adminMenu(
     *     'name'   => '删除运费管理',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '删除运费管理',
     *     'param'  => ''
     * )
     */
    public function del_freight_tpl()
    {
        $param=request()->param();
        $freight_id=$param['id'];
        $freight=new FreightModel();
        $freight_tpl=new FreightTplModel();
        $rs=$freight->where('tpl_id',$freight_id)->delete();
        $rs=$freight_tpl->where('id',$freight_id)->delete();
        $this->success('删除成功');

    }
    /**
     * 运费配置
     * @adminMenu(
     *     'name'   => '运费配置',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '运费配置',
     *     'param'  => ''
     * )
     */
    public function index_freight()
    {
        $param=request()->param();
        if($param){
            session('freight_info',$param);
        }else{
            $param=session('freight_info');
        }

        $freight=new FreightModel();
        $freight_list=$freight->get_freight($param);
        $this->assign('param',$param);
        $this->assign('freight_list',$freight_list);
        return $this->fetch();
    }
    /**
     * 添加运费配置
     * @adminMenu(
     *     'name'   => '添加运费配置',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '添加运费配置',
     *     'param'  => ''
     * )
     */
    public function add_freight()
    {
        $express = new ExpressService();
        $address= new AddressService();
        if (request()->isAjax()) {
            $retval = - 1;
            $data = request()->post("data", "");

            $data=htmlspecialchars_decode($data);
            $json_data = json_decode($data, true);
            $tpl_id= $json_data['tpl_id'];
            $shipping_fee_id = $json_data['shipping_fee_id']; // 0：添加，大于0：修改
            $shipping_fee_name = $json_data['shipping_fee_name']; // 运费模板名称
            $province_id_array = $json_data['province_id_array']; // 省id组
            $city_id_array = $json_data['city_id_array']; // 市id组
            $district_id_array = $json_data["district_id_array"]; // 区县id组

            $weight_snum = $json_data['weight_snum']; // 首重
            $weight_sprice = $json_data['weight_sprice']; // 首重运费
            $weight_xnum = $json_data['weight_xnum']; // 续重
            $weight_xprice = $json_data['weight_xprice']; // 续重运费

            $volume_snum = $json_data['volume_snum']; // 首体积量
            $volume_sprice = $json_data['volume_sprice']; // 首体积运费
            $volume_xnum = $json_data['volume_xnum']; // 续体积量
            $volume_xprice = $json_data['volume_xprice']; // 续体积运费

            $bynum_snum = $json_data['bynum_snum']; // 首件
            $bynum_sprice = $json_data['bynum_sprice']; // 首件运费
            $bynum_xnum = $json_data['bynum_xnum']; // 续件
            $bynum_xprice = $json_data['bynum_xprice']; // 续件运费
            $data_index= $json_data['data_index']; // 是否全国
            if ($shipping_fee_id) {
                $retval = $express->updateShippingFee($tpl_id,$shipping_fee_id,  $shipping_fee_name, $province_id_array, $city_id_array, $district_id_array, $weight_snum, $weight_sprice, $weight_xnum, $weight_xprice,  $volume_snum, $volume_sprice, $volume_xnum, $volume_xprice,  $bynum_snum, $bynum_sprice, $bynum_xnum, $bynum_xprice,$data_index);
            } else {
                $retval = $express->addShippingFee( $tpl_id,$shipping_fee_name, $province_id_array, $city_id_array, $district_id_array,  $weight_snum, $weight_sprice, $weight_xnum, $weight_xprice,  $volume_snum, $volume_sprice, $volume_xnum, $volume_xprice,  $bynum_snum, $bynum_sprice, $bynum_xnum, $bynum_xprice,$data_index);
            }
            return AjaxReturn($retval);
        } else {
            $param=request()->param();
            $freight=new FreightModel();
            $district=[];
            if($param['type']==1){
                $where['tpl_id']=$param['tpl_id'];
                $where['is_whole']=0;
                $rs=$freight->where($where)->find();
                if($rs){
                    $this->redirect(url('admin/freight/edit_freight',array('id'=>$rs['shipping_fee_id'],'tpl_id'=>$rs['tpl_id'],'type'=>1)));
                }
            }else{
                $where['tpl_id']=$param['tpl_id'];
                $where['is_whole']=1;
                $yi_info=$freight->where($where)->select()->toArray();
                if($yi_info){
                    foreach($yi_info as $key=>$val){
                        $son_district=$val['district_id_array'];
                        $son_district=explode(',',$son_district);
                        $district=array_keys(array_flip($district)+array_flip($son_district));
                    }
                }
            }

            $address_list = $address->getAreaTree_ext($district);
            $this->assign("param", $param);
            $this->assign("address_list", $address_list);
            return $this->fetch();
        }
    }
    /**
     * 编辑运费配置
     * @adminMenu(
     *     'name'   => '编辑运费配置',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '编辑运费配置',
     *     'param'  => ''
     * )
     */
    public function edit_freight()
    {
        $param=request()->param();
        $express = new ExpressService();
        $address= new AddressService();
        if(request()->isAjax()){
            //不用写了
        }else{
            $id=$param['id'];
            $freight=new FreightModel();
            $info=$freight->find($id);
            $where['tpl_id']=$info['tpl_id'];
            $where['is_whole']=1;
            $where['shipping_fee_id']=['<>',$id];
            $yi_info=$freight->where($where)->select()->toArray();
            $district=[];
            if($yi_info){
                foreach($yi_info as $key=>$val){
                    $son_district=$val['district_id_array'];
                    $son_district=explode(',',$son_district);
                    $district= array_keys(array_flip($son_district)+array_flip($district));
                }
            }
            $this->assign('info',$info);
            $address_list = $address->getAreaTree_ext($district);
            $this->assign("param", $param);
            $this->assign("address_list", $address_list);
            return $this->fetch();
        }


    }
    /**
     * 删除运费配置模版
     * @adminMenu(
     *     'name'   => '删除运费配置模版',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '删除运费配置模版',
     *     'param'  => ''
     * )
     */
    public function del_freight()
    {
        $param=request()->param();
        $freight_id=$param['id'];
        $freight=new FreightModel();
        $rs=$freight->where('shipping_fee_id',$freight_id)->delete();
        $this->success('删除成功');
    }


    /**
     * 物流管理
     * @adminMenu(
     *     'name'   => '物流管理',
     *     'parent' => 'admin/Setting/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '物流管理',
     *     'param'  => ''
     * )
     */
    public function logistics_index()
    {
        $logistics=new LogisticsModel();
        $logistics_list=$logistics->get_list();
        $this->assign('logistics_list',$logistics_list);
        return $this->fetch('logistics_index');
    }

    function add_logistics(){
        if(request()->isPost()){
            $param=request()->param();
            $logistics=new LogisticsModel();
            $logistics->save($param);
            $this->success('添加成功',url('admin/freight/logistics_index'));
        }else{
            return $this->fetch();
        }
    }
    function edit_logistics(){
        $logistics=new LogisticsModel();
        if(request()->isPost()){
            $param=request()->param();
            $id=$param['id'];
            unset($param['id']);
            $logistics=new LogisticsModel();
            $logistics->where('id',$id)->update($param);
            $this->success('编辑成功',url('admin/freight/logistics_index'));
        }else{
            $param=request()->param();
            $logistics_info=$logistics->find($param['id']);
            $this->assign('logistics_info',$logistics_info);
            return $this->fetch();
        }
    }
    function del_logistics(){
        $param=request()->param();
        $id=$param['id'];
        $logistics=new LogisticsModel();
        $logistics->where('id',$id)->delete();
        $this->success('删除成功',url('admin/freight/logistics_index'));
    }

}